﻿



#include <string>
#include <vector>

#include "ExcelUnit.h"
#include "EXData.h"


dn_namespace_beg

namespace
{
	bool SplitFillInPath(const std::string& FillInPath, std::string& Path,std::string& FileName)
	{
		std::string::const_iterator LastDiagonalIt = FillInPath.begin();

		std::string::const_iterator it = FillInPath.begin();
		std::string::const_iterator it_end = FillInPath.end();

		for(;it!=it_end;it++)
		{
			if(*it=='\\')
				LastDiagonalIt = it;
		}

		it = FillInPath.begin();
		for(;it!=LastDiagonalIt;it++)
		{
			Path.push_back(*it);
		}

		LastDiagonalIt++;

		it = LastDiagonalIt;
		for(;it!=it_end;it++)
		{
			FileName.push_back(*it);
		}

		return true;
	}

	ExcelTable* LoadEXD(std::string FillInPath)
	{
		std::string FilePath;
		std::string FileName;

		if(!SplitFillInPath( FillInPath, FilePath, FileName))
			return NULL;

		ExcelTable* pIndexTable = new ExcelTable();
		if(!pIndexTable->LoadTable(FilePath,FileName))
			return NULL;

		return pIndexTable;
	}

	void EndLoadEXD(ExcelTable* pTable)
	{
		if(pTable)
		{
			delete pTable;
			pTable = NULL;
		}
	}

}

void* EXD_Line_Base::get(int index){

	if(index < (int)dataVec.size()){
		return dataVec[index];
	}else {
		return 0;
	}
}


EXD_Table_Base::~EXD_Table_Base()
{
	std::map<int,EXD_Line_Base*>::iterator it = ID_LineMap.begin();
	std::map<int,EXD_Line_Base*>::iterator it_end = ID_LineMap.end();

	for(;it!=it_end;it++)
	{
		delete it->second;
		it->second = NULL;
	}
}

EXD_Line_Base* EXD_Table_Base::find(int ID)
{
	std::map<int,EXD_Line_Base*>::iterator it = ID_LineMap.find(ID);
	if(it==ID_LineMap.end())
	{
		return NULL;
	}
	else
	{
		return it->second;
	}
}

EXD_Line_Base* EXD_Table_Base::get(int Idx)
{
	std::map<int,EXD_Line_Base*>::iterator it = Idx_LineMap.find(Idx);
	if(it==Idx_LineMap.end())
	{
		return NULL;
	}
	else
	{
		return it->second;
	}
}

size_t EXD_Table_Base::size()
{
	return Idx_LineMap.size();
}

//--------------------------------------------------------------------------

#define EXD_DEFINE_BEGIN(ExcelName) \
static EXD_Table_##ExcelName ST_##ExcelName;\
EXD_Table_##ExcelName* GT_##ExcelName = &ST_##ExcelName;\
\
EXD_Line_##ExcelName* EXD_Table_##ExcelName::find(int ID)\
{\
	return (EXD_Line_##ExcelName*)EXD_Table_Base::find(ID);\
}\
\
EXD_Line_##ExcelName* EXD_Table_##ExcelName::get(int Idx)\
{\
	return (EXD_Line_##ExcelName*)EXD_Table_Base::get(Idx);\
}\
\
bool EXD_Table_##ExcelName::Build(std::string FillInPath)\
{\
	ExcelTable* pIndexTable = LoadEXD(FillInPath);\
	if(!pIndexTable)\
	{\
		return false;\
	}\
	\
	ExcelLineMap::iterator it		= pIndexTable->LineMap_Index.begin();\
	ExcelLineMap::iterator it_end	= pIndexTable->LineMap_Index.end();\
	for(int idx =0;it!=it_end;it++,idx++)\
	{\
		ExcelLine* pLine = it->second;\
		if(!pLine)\
			continue;\
			\
		EXD_Line_##ExcelName* pExcelLineData = new EXD_Line_##ExcelName;\
		int count = 0;\

#define EXD_VALUE(Type,Name)	pLine->GetEXDValue(count ,pExcelLineData->Name);\
		pExcelLineData->dataVec.push_back((void*)(&pExcelLineData->Name));\
		Row_##Name = count;\
		count++;\

#define EXD_DEFINE_END(ExcelName)	ID_LineMap[pLine->ID] =pExcelLineData;\
		Idx_LineMap[pLine->Index] =pExcelLineData;\
		pExcelLineData->LineIdx = pLine->Index;\
	}\
	EndLoadEXD(pIndexTable);\
	return true;\
}\

#include "DefineInc.h"
#include "DefineInc_End.h"

/*static EXD_Table_excel_index ST_excel_index;
EXD_Table_excel_index* GT_excel_index = &ST_excel_index;

EXD_Line_excel_index* EXD_Table_excel_index::find(int ID)
{
	return (EXD_Line_excel_index*)EXD_Table_Base::find(ID);
}

EXD_Line_excel_index* EXD_Table_excel_index::get(int Idx)
{
	return (EXD_Line_excel_index*)EXD_Table_Base::get(Idx);
}

bool EXD_Table_excel_index::Build(std::string FillInPath)
{
	ExcelTable* pIndexTable = LoadEXD(FillInPath);
	if(!pIndexTable)
	{
		return false;
	}

	ExcelLineMap::iterator it		= pIndexTable->LineMap_Index.begin();
	ExcelLineMap::iterator it_end	= pIndexTable->LineMap_Index.end();
	for(int idx =0;it!=it_end;it++,idx++)
	{
		ExcelLine* pLine = it->second;
		if(!pLine)
			continue;

		EXD_Line_excel_index* pExcelLineData = new EXD_Line_excel_index;

		int count = 0;
		pLine->GetStr(count ,pExcelLineData->name);

		count++;
		pLine->GetInt(count ,pExcelLineData->id);

		count++;
		pLine->GetStr(count ,pExcelLineData->string_test);

		count++;
		pLine->GetInt(count ,pExcelLineData->int_test);

		count++;
		pLine->GetFloat(count ,pExcelLineData->float_test);

		count++;
		pLine->GetArrayStr(count ,pExcelLineData->v_string_test);

		count++;
		pLine->GetArrayInt(count ,pExcelLineData->v_int_test);

		count++;
		pLine->GetArrayFloat(count ,pExcelLineData->v_float_test);	
		

		ID_LineMap[pLine->ID] =pExcelLineData;
		Idx_LineMap[pLine->Index] =pExcelLineData;
	}

	EndLoadEXD(pIndexTable);

	return true;

}*/



dn_namespace_end


